package com.agile.leetcode.hot.middle;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/7/26 9:51
 */
public class RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 创建新节点指向头部
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        while (head != null) {
            // 创建新的链表
            ListNode temp = new ListNode(head.val, head.next);
            for (int i = 0; i < n; i++) {
                temp = temp.next;
            }
            // 找到了
            if (temp == null) {
                current.next = head.next;
                break;
            } else {
                current.next = head;
            }
            head = head.next;
            current = current.next;
        }
        return dummy.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public ListNode() {
    }
}
